-- 运营活动: 充值充值


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动 
    act_id = 92, -- 活动id (92 新重复充值)
    act_name = "新重复充值", -- 活动名称
    act_desc = "警告！嗜血魔物会在指定时间来袭！", -- 活动描述
    beg_time = 1528819200,  -- 活动开始时间(unix timestamp)
    end_time = 1594044800,  -- 活动结束时间(unix timestamp)
    config = -- 配置数据
    {
        needRechargeGold = 100,
        maxRechargeCount = 10,
        Rewards =       --充值奖励
        {
            {
                --第一档奖
                {type = 0, id = 763, count =20, bind = 1,},
                {type = 0, id = 645, count =20, bind = 1,},
                {type = 0, id = 517, count = 3, bind = 1,},
                {type = 0, id = 592, count = 3, bind = 1,},
            },
            {
                --第二档奖励
                {type = 0, id = 763, count =33, bind = 1,},
                {type = 0, id = 644, count = 1, bind = 1,},
                {type = 0, id = 645, count =50, bind = 1,},
                {type = 0, id = 517, count = 5, bind = 1,},
                {type = 0, id = 592, count = 7, bind = 1,},
            },
            {   
                --第三档奖励
                {type = 0, id = 763, count =70, bind = 1,},
                {type = 0, id = 533, count = 5, bind = 1,},
                {type = 0, id = 538, count = 5, bind = 1,},
                {type = 0, id = 644, count = 1, bind = 1,},
                {type = 0, id = 517, count = 5, bind = 1,},
                {type = 0, id = 592, count = 1, bind = 1,},
            },
        },      
    },
}
]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_92_check_cfg(cfg)
    if cfg == nil then return 20 end
    
    local config = cfg.config
    if config == nil then return 21 end

    return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_92_setup(cfg) 
    return 0
end

-- 卸载活动
function oper_act_92_unsetup(cfg)   
end

-- 活动开始
function oper_act_92_start(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_92_start = 1
    ssVar.oper_act_92_cmd = cfg.cmd_id
    ssVar.oper_act_92_act = cfg.act_id

    local data = gRankMgr_GetValueByIndx(0, RANK_DEFINE_OPERATION_92, 1)

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            return
        end
    end

    gRankMgr_Clear(RANK_DEFINE_OPERATION_92)
end

-- 活动结束
function oper_act_92_end(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_92_start = nil
    ssVar.oper_act_92_cmd = nil
    ssVar.oper_act_92_act = nil
end

-- 打包数据
function oper_act_92_packet_data(sysarg, cfg)

    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_92_start then
        return
    end
    if not cfg then
        return
    end

    local config = cfg.config
    if not config then
        return
    end

    
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_92, 1)
    local myData = BaseFuc_unserialize(data)

    if not myData or myData == "" or myData == 0 then
        myData = {}
    end

    if not myData.myValue then
        myData.myValue = 0
    end

    local time = System.getSystemTime()
    if myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
        myData.myBuyLimit = {}
    end

    if not myData.myBuyLimit then
        myData.myBuyLimit = {}
    end

    if not myData.myPrecent then
        myData.myPrecent = {}
    end


    local pack = oper_act_alloc_init_data_packet(sysarg, cfg.cmd_id, cfg.act_id)
    if (pack == nil) then
        return
    end

    DataPack.writeUInt(pack, myData.myValue)
    local len = #config.itemList

    DataPack.writeByte(pack, len)
    for idx = 1, len do
        local item = config.itemList[idx]
        DataPack.writeByte(pack, idx)
        local num = item.limitNum - (myData.myBuyLimit[idx] or 0)
        DataPack.writeShort(pack, num)
    end

    local len2 = #config.Awards
    DataPack.writeByte(pack, len2)
    for idx = 1, len2 do
        local item = config.Awards[idx]
        if not myData.myPrecent[idx] then
            myData.myPrecent[idx] = {}
        end
        DataPack.writeByte(pack, #item.precentBack)
        for ii = 1, #item.precentBack do
            DataPack.writeByte(pack, myData.myPrecent[idx][ii] or 0)
        end
    end

    DataPack.flush(pack)

    --DataPack.writeByte(netPack, 1)        -- 活动特有参数
end


-- 操作，返回操作结果，0为成功
function oper_act_92_do(sysarg, cfg, clientPkt)
    if cfg == nil then return end
    
    if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
        return
    end
    
    local config = cfg.config
    if config == nil then -- 参数错误
        print("error, oper_act_92_do cfg.config confg error")
        return
    end

    local nIndex = DataPack.readByte( clientPkt )


    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_92, 1)

    local myData = BaseFuc_unserialize(data)
    

    if not myData or myData == "" or myData == 0 then
        myData = {}
    end

    if not myData.myValue then
        myData.myValue = 0
    end

    local time = System.getSystemTime()
    if myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
        myData.myBuyLimit = {}
    end

    if not myData.myBuyLimit then
        myData.myBuyLimit = {}
    end

    if not myData.myPrecent then
        myData.myPrecent = {}
    end

    local result = 0    

    local item = config.itemList[nIndex]

    if not item then
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation015, ttFlyTip)
        return
    end

    if myData.myBuyLimit[nIndex] and item.limitNum <= myData.myBuyLimit[nIndex] then
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation016, ttFlyTip)
        return
    end

    if item.needScore > myData.myValue then
        Actor.sendTipmsg(sysarg,string.format(Lang.ScriptTips.Operation017, item.needScore), ttFlyTip)
        return
    end

    if not myData.myBuyLimit[nIndex] then
        myData.myBuyLimit[nIndex] = 0
    end

    myData.myBuyLimit[nIndex] = myData.myBuyLimit[nIndex] + 1
    myData.myValue = myData.myValue - item.needScore

    if not myData.myPrecent[item.awardId] then
        myData.myPrecent[item.awardId] = {}
    end

    if not myData.myPrecent[item.awardId][item.rateId] then
        myData.myPrecent[item.awardId][item.rateId] = 0
    end

    myData.myPrecent[item.awardId][item.rateId] = myData.myPrecent[item.awardId][item.rateId] + 1

    if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = time
    end

    myData.mySelfTime = time

    local newData = BaseFuc_serialize(myData)

    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_92, 1, newData)

    oper_act_92_packet_data(sysarg, cfg)
end


--[[
-- 跨天
function oper_act_2_new_day_arrive(act_id)  
    
end
]]

--限时商品元宝
function oper_act_92_consumeyuanbao(sysarg, Gold)
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_92_start then
        return
    end
    if not ssVar.oper_act_92_cmd then
        return
    end
    if not ssVar.oper_act_92_act then
        return
    end
    
    local cfg = get_oper_act_cfg(ssVar.oper_act_92_cmd, ssVar.oper_act_92_act)
    if not cfg then
        return
    end

    local config = cfg.config
    if not config then
        return
    end 

    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_92, 1)
    local myData = BaseFuc_unserialize(data)

    if not myData or myData == "" or myData == 0 then
        myData = {}
    end

    if not myData.myValue then
        myData.myValue = 0
    end

    myData.myValue = myData.myValue + Gold

    if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = System.getSystemTime()
    end

    local newData = BaseFuc_serialize(myData)

    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_92, 1, newData)
        
    oper_act_92_packet_data(sysarg, cfg)
            
end


--限时商品元宝
function oper_act_92_withdrawyuanbao(sysarg, Gold)
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_92_start then
        return
    end
    if not ssVar.oper_act_92_cmd then
        return
    end
    if not ssVar.oper_act_92_act then
        return
    end
    
    local cfg = get_oper_act_cfg(ssVar.oper_act_92_cmd, ssVar.oper_act_92_act)
    if not cfg then
        return
    end

    local config = cfg.config
    if not config then
        return
    end 

    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_92, 1)
    local myData = BaseFuc_unserialize(data)

    if not myData or myData == "" or myData == 0 then
        myData = {}
    end

    if not myData.myPrecent then
        myData.myPrecent = {}
    end

    for idx = 1, #config.Awards do
        if config.Awards[idx].needRechargeGold == Gold then
            if myData.myPrecent[idx] then
                for ii = 1, #config.Awards[idx].precentBack do
                    if myData.myPrecent[idx][ii] and myData.myPrecent[idx][ii] > 0 then
                        myData.myPrecent[idx][ii] = myData.myPrecent[idx][ii] - 1
                        local payBack = math.floor(config.Awards[idx].precentBack[ii].backRate * Gold / 100)
                        local awardType = config.awardType
                        local rewards = {{ type = awardType, count = payBack, bind = 0},}

                        local title = string.format(Lang.ScriptTips.Operation202, cfg.act_name)
                        local content = string.format(Lang.ScriptTips.Operation236, cfg.act_name)
                        Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OperationLogId092, Lang.LogTips.Operation092)
                        if not myData.cmd_id then
                            myData.cmd_id = cfg.cmd_id
                            myData.act_id = cfg.act_id
                            myData.time = System.getSystemTime()
                        end

                        local newData = BaseFuc_serialize(myData)

                        gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_92, 1, newData)
                            
                        oper_act_92_packet_data(sysarg, cfg)
                        return
                    end
                end
            end
        end
    end     
end



